student1 = {
'firstname': 'Marissa',
'surname': 'Taggart',
'age': 22,
'major': 'Economics'
}
student2 = {
'firstname': 'Martin',
'surname': 'Talmage',
'age': 23,
'major': 'Spanish Literature'
}
NOTES
students = [
{'firstname': 'Juan', 'surname': 'Lopez', 'age': 18, 'major': 'Linguistics'},
{'firstname': 'Ulysses', 'surname': 'Bennion', 'age': 25, 'major': 'Mechanical Engineering'},
{'firstname': 'Sarah', 'surname': 'Grover', 'age': 19, 'major': 'Mathematics'},
{'firstname': 'Mary', 'surname': 'Han', 'age': 20, 'major': 'Nursing'},
{'firstname': 'Jacob', 'surname': 'Smith', 'age': 18, 'major': 'Open Major'}
]
# who is the oldest student in the class?
def get_oldest(students):
oldest = None
for student in students:
if oldest is None or student['age'] > oldest['age']:
oldest = student
return oldest
oldest = get_oldest(students)
print(oldest)
# Do we have any math majors?
def has_math_major(students):
for student in students:
if student['major'] == 'Mathematics':
return True
return False
print(has_math_major(students))
Functions give us the ability to create abstractions for actions.
Data objects give us the ability to create abstractions for information.
NOTES
In typed languages (like C++, C#, or Java) you typically define the properties of a data abstraction in the code.
In untyped languages (like Python or Javascript) you typically do not define the properties of a data abstraction in the code.
NOTES
The definition of a data abstraction is known as the type definition, schema, or shape.
def print_eligible_students(students):
# Students must be part of the Physics major and be at least 21 years old
for student in students:
if student['major'] == 'Physics' and student['age'] >= 21:
print(f"{student['last']}, {student['first']} ({student['standing']})")
NOTES
Student
json
¶students
import json
with open('students.json', 'w') as file:
json.dump(students, file)
students.json
¶! cat students.json
import json
with open('students.json', 'w') as file:
json.dump(students, file, indent=2)
! cat students.json
students.json
¶Notes
import json
with open('students.json') as file:
student_info = json.load(file)
print(student_info)
print(student_info[0])
pokedex.json
¶NOTES
import json
def find_largest_hp(pokedex):
largest_hp = None
for pokemon in pokedex:
if largest_hp is None or pokemon['base']['HP'] > largest_hp['base']['HP']:
largest_hp = pokemon
return largest_hp
with open('pokedex.json') as file:
pokedex = json.load(file)
largest_hp = find_largest_hp(pokedex)
print(largest_hp)
import json
def group_by_type(pokedex):
groups = {}
for pokemon in pokedex:
for tp in pokemon['type']:
if tp not in groups:
groups[tp] = []
groups[tp].append(pokemon)
return groups
def find_smallest_group(groups):
smallest = None
smallest_type = None
for tp, group in groups.items():
if smallest is None or len(group) < len(smallest):
smallest = group
smallest_type = tp
return smallest_type, smallest
def find_rarest_type(pokedex):
groups = group_by_type(pokedex)
return find_smallest_group(groups)
with open('pokedex.json') as file:
pokedex = json.load(file)
name, group = find_rarest_type(pokedex)
print(name, len(group))
json